package cn.edu.ctbu.api01.restcontroller;


import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.exception.RException;
import cn.edu.ctbu.api01.helper.R;
import cn.edu.ctbu.api01.helper.PageUtils;
import cn.edu.ctbu.api01.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Pageable;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/webapi/student")
public class StudentRestController {

    @Autowired
    private StudentService studentService;

    /**
     * 获取所有学生的信息
     * url: /webapi/student/list
     * @return pageUtils
     */
    @GetMapping("/list")
    public PageUtils GetAll(Integer pageSize, Integer pageNum){
        Pageable pageable = PageRequest.of(pageNum-1, pageSize);
        Page<Student> tempStudents = studentService.getAll(pageable);
        PageUtils pageUtils = new PageUtils(tempStudents.getTotalElements(),tempStudents.getContent());
        return  pageUtils;
    }

    /**
     * 根据姓名查询学生信息
     * url: /webapi/student/getbyname/{name}
     * @param name 学生姓名
     * @return pageUtils
     */
    @GetMapping(value = "/getbyname/{name}")
    public PageUtils findByName(Integer pageSize, Integer pageNum, @PathVariable String name) {
        Pageable pageable = PageRequest.of(pageNum-1, pageSize);
        Page<Student> tempStudents = studentService.findByName(name, pageable);
        PageUtils pageUtils = new PageUtils(tempStudents.getTotalElements(),tempStudents.getContent());
        return  pageUtils;
    }

    /**
     * 根据学号查询学生信息
     * url：/webapi/student/getbyno/{no}
     * @param no 学生学号
     * @return pageUtils
     */
    @GetMapping(value = "/getbyno/{no}")
    public PageUtils findByNo(Integer pageSize, Integer pageNum, @PathVariable String no){
        Pageable pageable = PageRequest.of(pageNum-1, pageSize);
        Page<Student> tempStudents = studentService.findByNo(no, pageable);
        PageUtils pageUtils = new PageUtils(tempStudents.getTotalElements(),tempStudents.getContent());
        return  pageUtils;
    }

    /**
     * 根据id查询学生信息
     * url：/webapi/student/getbyid/{id}
     * @param id 学生id
     * @return pageUtils
     */
    @GetMapping(value = "/getbyid/{id}")
    public PageUtils findById(Integer pageSize, Integer pageNum, @PathVariable Integer id){
        Pageable pageable = PageRequest.of(pageNum-1, pageSize);
        Page<Student> tempStudents = studentService.findById(id, pageable);
        PageUtils pageUtils = new PageUtils(tempStudents.getTotalElements(),tempStudents.getContent());
        return  pageUtils;
    }

    /**
     * 获取要编辑的学生信息
     * url：/webapi/student/get/id
     * @param id 学生id
     * @return student
     */
    @GetMapping(value = "/get/{id}")
    public Student get(@PathVariable Integer id){
        Student student = studentService.findById(id);
        student.setPassword("");
        return student;
    }

    /**
     * 更新学生信息
     * url: /webapi/student/update
     * @param student 学生对象，包含学生信息
     * @return studentService.update(student)
     */
    @PostMapping("/update")
    public Student update(Student student) {
        // 读取旧的学生信息
        Student oldstudent = studentService.findById(student.getId());
        // 如果没有设置新的密码，就继续用原来的密码
        if (StringUtils.isEmpty(student.getPassword())){
            student.setPassword(oldstudent.getPassword());
        }
        // 返回学生信息
        return studentService.update(student);
    }

    /**
     * 插入学生信息
     * url: /webapi/insert
     * @param student 学生对象，包含学生信息
     * @return student.getId()
     */
    @PostMapping("/insert")
    public int insert(@Valid Student student, BindingResult bindingResult) {
        // 验证插入的信息是否符合定义的规则
        if (bindingResult.hasErrors()) {
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return -1;
        }
        studentService.insert(student);
        return student.getId();
    }

    /**
     * 删除学生信息 对应/web/api/delete
     * @param id 学生id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        Student student = studentService.findById(id);
        studentService.delete(student);
    }

    @GetMapping(value = "/login")
    public R login(String username, String password) throws Exception, RException {

        studentService.validUserNameAndPassword(username, password);

        return R.ok();
    }


}
